package com.jess.arms.di.module;

import android.content.Context;
import android.net.ParseException;

import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.JsonIOException;
import com.google.gson.JsonParseException;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import me.jessyan.rxerrorhandler.handler.listener.ResponseErrorListener;
import retrofit2.HttpException;
import timber.log.Timber;

/**
 * ================================================
 * 展示 {@link ResponseErrorListener} 的用法
 * <p>
 * Created by MVPArmsTemplate on 01/07/2019 17:36
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * ================================================
 */
public class ResponseErrorListenerImpl implements ResponseErrorListener {

    @Override
    public void handleResponseError(Context context, Throwable t) {
        Timber.tag("Catch-Error").w(t.getMessage());
        //这里不光只能打印错误, 还可以根据不同的错误做出不同的逻辑处理
        //这里只是对几个常用错误进行简单的处理, 展示这个类的用法, 在实际开发中请您自行对更多错误进行更严谨的处理
        String msg = "未知错误,请联系系统管理员.";
        if (t instanceof UnknownHostException) {
            msg = "服务器不可用，请检查网络连接.";
        } else if (t instanceof ConnectException) {
            msg = "无法连接到服务器，请检查网络.";
        } else if (t instanceof SocketTimeoutException) {
            msg = "服务器无响应,请稍后重试.";
        } else if (t instanceof HttpException) {
            HttpException httpException = (HttpException) t;
            msg = convertStatusCode(httpException);
        } else if (t instanceof JsonParseException || t instanceof ParseException || t instanceof JSONException || t instanceof JsonIOException) {
            msg = "数据解析错误,请联系系统管理员.";
        }else {
            msg += "\n" + (t.getMessage() != null ? t.getMessage() : "");
        }
        //        ArmsUtils.snackbarText(msg);
        ToastUtils.showShort(msg);
    }

    private String convertStatusCode(HttpException httpException) {
        String msg;
        if (httpException.code() == 500) {
            try {//具体错误信息
                String exceptionMsg = httpException.response().errorBody().string();
                JSONObject json = new JSONObject(exceptionMsg);
                if (json.has("message"))
                    msg = json.getString("message");
                else
                    msg = "服务器内部错误,请联系系统管理员";
            } catch (Exception e) {
                msg = "服务器内部错误,请联系系统管理员";
                e.printStackTrace();
            }
        } else if (httpException.code() == 404) {
            msg = "接口地址不存在,请联系系统管理员";
        } else if (httpException.code() == 403) {
            msg = "请求被服务器拒绝,请联系系统管理员";
        } else if (httpException.code() == 307 || httpException.code() == 302) {
            //  msg = "请求重定向异常,请联系系统管理员";
            msg = "系统异常,请联系管理员";
        } else {
            msg = httpException.getMessage();
        }
        return msg;
    }
}
